package cn.iwit;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.iwit.util.BusiException;
import cn.iwit.util.JsonResult;

@ControllerAdvice
public class GlobalControllerExceptionHandler {
    private static final Logger LOGGER = LoggerFactory.getLogger(GlobalControllerExceptionHandler.class);

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public JsonResult exception(HttpServletRequest request, Exception e) {
    	 JsonResult jsonResult = new JsonResult();
    	 jsonResult.setCode("ZG0000");
    	 jsonResult.setMessage( e.getMessage());
         return jsonResult;
    }

    @ExceptionHandler(BusiException.class)
    @ResponseBody
    public JsonResult serviceException(HttpServletRequest request, BusiException e) {
    	 JsonResult jsonResult = new JsonResult();
         Map<String, Object> result = new HashMap<String, Object>();
         result.put("result", 1);
         result.put("status", 500);
         result.put("url", request.getRequestURL());
         result.put("timestamp", new Date().toString());
         jsonResult.setResult(1);
         jsonResult.setMessage( e.getMessage());
         jsonResult.setData(result);
         jsonResult.setCode(e.getErrcode());
        return jsonResult;
    }
}